
$(document).ready(function() {
    $('.questions').click(function(){
        $('.questions').removeClass('red-text');
        $('.red-text').removeClass('red-text');
        $(this).parent().addClass( 'red-text' );
        $(this). addClass('red-text');
        var content = $(this).html();
        var num = $(this).attr('rel');
        var answer = $('.answer' + num).val();
        $('.question-details').html(num + '. ' + content);
        $('.answer-details').html(answer);
    });

    $('#selectCategory').change(function(){
        var e = document.getElementById("selectCategory");
        var catId = e.options[e.selectedIndex].value;
        $.get( "/admin/category/subcat/catId/" + catId, function( data ) {
            $( ".selectSubCategory").html( data );
        });
    });

    $('.update-order-status').change(function() {
        var status = $(".update-order-status").val();
        var orderId = $(this).attr('rel');
        console.log(orderId);

        $.post( "/admin/order/update/id/" + orderId + '/status/' + status, function( data ) {

        });
    });

    $('.delete-order').click(function() {
        var selected = new Array();
        $('input:checkbox.ckb-del').each(function () {
            var sThisVal = (this.checked ? $(this).val() : "");
            selected.push(sThisVal);
        });

        $.post( "/admin/order/delete",{ ids: selected }, function( data ) {
            window.location.href = "/admin/order/admin";
        });
    });

    $('.delete-user').click(function() {
        var selected = new Array();
        $('input:checkbox.ckb-del').each(function () {
            var sThisVal = (this.checked ? $(this).val() : "");
            selected.push(sThisVal);
        });

        $.post( "/admin/users/delete",{ ids: selected }, function( data ) {
            window.location.href = "/admin/users/index";
        });
    });
    
    $('#frm-category .icon-circle-arrow-down').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertAfter(row.next());
        var catId = $(this).attr('rel');
        $.ajax({
            url: '/admin/category/updateorder',
            type: 'POST',
            data: {catId: catId, down: 1},
            success: function(response) {
                //alert(response);
            }
        });
    });

    $('#frm-category .icon-circle-arrow-up').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertBefore(row.prev());
        var catId = $(this).attr('rel');
        $.ajax({
            url: '/admin/category/updateorder',
            type: 'POST',
            data: {catId: catId, up: 1},
            success: function(response) {
                //alert(response);
            }
        });
    });
    
    $('#frm-category .icon-save').click(function(){
        $('#frm-category').submit();
    });
    
    $('#frm-subcategory .icon-circle-arrow-down').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertAfter(row.next());
        var catId = $(this).attr('rel');
        $.ajax({
            url: '/admin/subcategory/updateorder',
            type: 'POST',
            data: {catId: catId, down: 1},
            success: function(response) {
                //alert(response);
            }
        });
    });

    $('#frm-subcategory .icon-circle-arrow-up').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertBefore(row.prev());
        var catId = $(this).attr('rel');
        $.ajax({
            url: '/admin/subcategory/updateorder',
            type: 'POST',
            data: {catId: catId, up: 1},
            success: function(response) {
                //alert(response);
            }
        });
    });
    
    $('#frm-subcategory .icon-save').click(function(){
        $('#frm-subcategory').submit();
    });
    
    $('#frm-product .icon-circle-arrow-down').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertAfter(row.next());
        var rel = $(this).attr('rel');
        var proId = rel.split('-');
        if(proId[1]) {
            $.ajax({
                url: '/admin/products/updateorder',
                type: 'POST',
                data: {proId: proId[0], proIdChange: proId[1]},
                success: function(response) {
                     location.reload();
                }
            });
        }
    });
    
    $('#frm-product .icon-circle-arrow-up').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertBefore(row.prev());
        var rel = $(this).attr('rel');
        var proId = rel.split('-');
        if(proId[1]) {
            $.ajax({
                url: '/admin/products/updateorder',
                type: 'POST',
                data: {proId: proId[0], proIdChange: proId[1]},
                success: function(response) {
                     location.reload();
                }
            });
        }
    });
    
    $('#frm-product .icon-save').click(function(){
        $('#frm-product').submit();
    });
    
    $('.deleteIcon').click(function(){
        var $this = $(this);
        var imgId = $this.attr('rel');
        $.ajax({
            url: '/admin/products/deleteimage',
            type: 'POST',
            data: {imgId: imgId},
            success: function(response) {
                $this.parent().parent().remove();
            }
        });
    });
    
    $('#frm-faq .icon-circle-arrow-down').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertAfter(row.next());
        var rel = $(this).attr('rel');
        var proId = rel.split('-');
        if(proId[1]) {
            $.ajax({
                url: '/admin/faq/updateorder',
                type: 'POST',
                data: {faqId: proId[0], faqIdChange: proId[1]},
                success: function(response) {
                     location.reload();
                }
            });
        }
    });
    
    $('#frm-faq .icon-circle-arrow-up').click(function(){
        var row = $(this).parents('.gradeX');
        row.insertBefore(row.prev());
        var rel = $(this).attr('rel');
        var proId = rel.split('-');
        if(proId[1]) {
            $.ajax({
                url: '/admin/faq/updateorder',
                type: 'POST',
                data: {faqId: proId[0], faqIdChange: proId[1]},
                success: function(response) {
                     location.reload();
                }
            });
        }
    });
    
    $('#frm-faq .icon-save').click(function(){
        $('#frm-faq').submit();
    });
    
    $('#zone .icon-remove').click(function(){
        var id = $(this).attr('rel');
        $.ajax({
            url: '/admin/zone/delete',
            type: 'POST',
            data: {id: id},
            success: function(response) {
                 if(response) location.reload();
            }
        });
    });
    
    $('#frm-news .icon-circle-arrow-down').click(function(){
        var rel = $(this).attr('rel');
        var proId = rel.split('-');
        if(proId[1]) {
            $.ajax({
                url: '/admin/news/updateorder',
                type: 'POST',
                data: {newsId: proId[0], newsIdChange: proId[1]},
                success: function(response) {
                    location.reload();
                }
            });
        }
    });
    
    $('#frm-news .icon-circle-arrow-up').click(function(){
        var rel = $(this).attr('rel');
        var proId = rel.split('-');
        if(proId[1]) {
            $.ajax({
                url: '/admin/news/updateorder',
                type: 'POST',
                data: {newsId: proId[0], newsIdChange: proId[1]},
                success: function(response) {
                    location.reload();
                }
            });
        }
    });
    
    $('#frm-news .icon-save').click(function(){
        $('#frm-news').submit();
    });
});
